import {
  ConnectedSocket,
  MessageBody,
  SubscribeMessage,
  WebSocketGateway
} from '@nestjs/websockets'
import * as WebSocket from 'ws'
import * as colors from 'colors'
import { wsService } from './ws.service'
import { WealthService } from 'src/eastWealth/wealth.service'

/* webSocket */
/* gateway 网关 */
/* 指定 */
@WebSocketGateway(3536, {
  // namespace: 'events',
  // transports: ['websocket']
})
export class WsGateway {
  constructor(){}
  @SubscribeMessage('helloSock')
  async helloSock(
    @MessageBody() data: any, 
    @ConnectedSocket() client: WebSocket
  ): Promise<any> {
    // console.log('收到消息 socket', colors.red(data), client )
    // console.log(colors.red('socket 接收到的信息 --->'), data)
    // const wsServices = new wsService(new WealthService())

    // 连接成功 发送的数据
    client.send(
      JSON.stringify({ 
        event: 'tmp', 
        data: '这里是个临时信息 helloSock 端口3536' 
      })
    );
    return {
      event: 'helloSock',
      data: { a: 1, data,  },
      "msg": "hello socket"
    }
  }
  
  /* SubscribeMessage 指定网关的名称 */
  @SubscribeMessage('hello2')
  hello2(@MessageBody() data: any, @ConnectedSocket() client: WebSocket): any {
    // console.log('收到的信息数据:', data);
    // client.send(JSON.stringify({ 
    //   event: 'tmp', 
    //   data: '这里是个临时信息rustfisher.com' + data
    // }));
    return { 
      event: 'hello2', 
      data: {
        ...data,
        data: new Date(Number(data)).toString()
      } 
    };
  }
}
